/*---------------------------------------------------
-----------------------------------------------------
Filename:	pledit.m
Version:	3.0

Type:		maki
Date:		07. Mrz. 2007 - 22:02 
Author:		Martin Poehlmann aka Deimos
E-Mail:		martin@skinconsortium.com
Internet:	www.skinconsortium.com
		www.martin.deimos.de.vu
-----------------------------------------------------
---------------------------------------------------*/

#include <lib/std.mi>

// #define DEBUG
#define FILE_NAME "pledit.m"
#include lib/debug.m

#define PL_GUID "{45F3F7C1-A6F3-4EE6-A15E-125E92FC3F8D}"

#include attribs/init_playlist.m

Function fitContent (int playlistw, int playlistx);
Function fit (group g, int x, int rx, int y, int ry, int w, int rw, int h, int rh);
Function int updatePoppler(int w);

Global Frame dualwnd;

Global Group g_playlist, g_player, g_sui, g_buttons, g_mcv;
Global Button p_enlarge, p_small;
Global Button p_resize;
Global int down_x, down_gx, isresizing;
Global layout main;
Global Container player;
Global WindowHolder wdh_pl;
Global Text pl_time;

Global Button pe_add, pe_rem, pe_sel, pe_misc, pe_manage;

Global Int resize_modulo;
Global Boolean startup;

Global Timer dc_openPl, dc_closePl, dc_loadWnd;

Global Int COMP_Y, COMP_H, SUI_Y, SUI_H, BOTTOMSPACER, MAX_PL_H, MIN_PL_W, MIN_PLAYER_W;
Global Int DEF_PL_W, SIDESPACER, MIN_REST_W;



System.onScriptLoaded ()
{
	initAttribs_Playlist();
	main = getScriptGroup();
	player = main.getContainer();

	dualwnd = main.getObject("player.dualwnd");
	g_mcv = dualwnd.findObject("player.component.fileinfo");
	g_playlist = dualwnd.findObject("player.component.playlist");
	g_player = dualwnd.findObject("player.layout");
	g_sui = main.getObject("sui.content");
	g_buttons = g_playlist.getObject("player.component.playlist.buttons");
	p_enlarge = g_buttons.getObject("player.playlist.enlarge");
	p_small = g_buttons.getObject("player.playlist.small");
	p_resize = g_playlist.getObject("player.resize");
	wdh_pl = g_playlist.getObject("wdh.playlist");
	pl_time = g_buttons.getObject("PLTime");

	pe_add = g_buttons.getObject("player.playlist.add");
	pe_rem = g_buttons.getObject("player.playlist.rem");
	pe_sel = g_buttons.getObject("player.playlist.sel");
	pe_misc = g_buttons.getObject("player.playlist.misc");
	pe_manage = g_buttons.getObject("player.playlist.manage");

	dc_openPl = new Timer;
	dc_openPl.setDelay(1);

	dc_closePl = new Timer;
	dc_closePl.setDelay(1);

	dc_loadWnd = new Timer;
	dc_loadWnd.setDelay(1);

	//	Global Defienitions:

	DEF_PL_W = g_playlist.getGuiW(); // (-)
	SIDESPACER = g_sui.getGuiX(); // (+)
	COMP_Y = dualwnd.getGuiY(); // (+)
	COMP_H = dualwnd.getGuiH(); // (+)
	SUI_Y = g_sui.getGuiY(); // (+)
	SUI_H = g_sui.getGuiH(); // (-)
	BOTTOMSPACER = SUI_Y + SUI_H; // (-)
	MIN_PLAYER_W = stringToInteger(getToken(getParam(), ",", 0)); // (+)
	MIN_PL_W = stringToInteger(getToken(getParam(), ",", 1)); // (+)
	MAX_PL_H = BOTTOMSPACER - COMP_Y; // (-)
	MIN_REST_W = stringToInteger(getToken(getParam(), ",", 2)); // (+)

	startup = 1;

	//dc_loadWnd.start();
}

dc_loadWnd.onTimer ()
{
	stop();
	int pos = dualwnd.getPosition();
	if (pos > 0) updatePoppler(pos);
	playlist_enlarge_attrib.onDataChanged();
	if (dualwnd.getPosition() > 0) dc_openPL.start();
	else hideWa2Component(PL_GUID);
}


playlist_enlarge_attrib.onDataChanged ()
{
	int pl_w = dualwnd.getPosition();

	int pl_x = 0 - ( pl_w + SIDESPACER ); // (-)

	if (getData() == "1")
	{
		int sui_w = pl_x - SIDESPACER; // (-)

		dualwnd.setXmlParam("h", integerToString(MAX_PL_H));
		dualwnd.setXmlParam("relath", "1");

		g_sui.setXmlParam("w", integerToString(sui_w-8));
		p_enlarge.hide();
		p_small.show();
	}
	else
	{
		int sui_w = 0 - SIDESPACER - SIDESPACER; // (-)
		int pc_w = pl_x - SIDESPACER; // (-)

		dualwnd.setXmlParam("h", integerToString(COMP_H));
		dualwnd.setXmlParam("relath", "0");
		g_sui.setXmlParam("w", integerToString(sui_w));
		p_enlarge.show();
		p_small.hide();
	}
}

g_playlist.onResize (int x, int y, int w, int h)
{
	int pos = dualwnd.getPosition();
	debugString(integerToString(pos), 9);
	if (pos > 0) 
	{
		//setPrivateInt(getSkinName(), "pledit poppler width", dualwnd.getPosition());
		if (!wdh_pl.isVisible()) dc_openPl.start();
	}
	else wdh_pl.hide();
	if (playlist_enlarge_attrib.getdata() == "1")
	{
		int sui_w = - g_playlist.getWidth() - (2 * SIDESPACER) - 8;
		g_sui.setXmlParam("w", integerToString(sui_w));
		if (startup)
		{
			startup = 0;
			dualwnd.setXmlParam("h", integerToString(MAX_PL_H));
			dualwnd.setXmlParam("relath", "1");
		}
	}
}

main.onSetVisible (Boolean onoff)
{
	if (onoff)
	{
		playlist_enlarge_attrib.onDataChanged();
		int pos = dualwnd.getPosition();
		if (pos > 0) updatePoppler(pos);
		if (dualwnd.getPosition() > 0) dc_openPL.start();
		else hideWa2Component(PL_GUID);
	}
}


main.onUserResize (int x, int y, int w, int h)
{
	int pos = dualwnd.getPosition();
	if (pos > 0) updatePoppler(pos);
}

/** Hide pl_time if it cannot be full displayed */


pl_time.onResize (int x, int y, int w, int h)
{
	if (w < getTextWidth())
	{
		hide();
	}
	else
	{
		show();
	}
}

pl_time.onTextChanged (String newtxt)
{
	if (getWidth() < getTextWidth())
	{
		hide();
	}
	else
	{
		show();
	}	
}


/** Playlist Component Handling */

System.onGetCancelComponent (String guid, boolean goingvisible)
{
	debugString(DEBUG_PREFIX "System.onGetCancelComponent ( "+ guid + " , " + integerToString(goingvisible) + " )", D_WTF);
	if (guid == PL_GUID)
	{
		Boolean isShade = (player.getCurLayout() != main);

		if (goingvisible == TRUE && !isShade)
		{	
			int poppler_w = getPrivateInt(getSkinName(), "pledit poppler width", 200);
			updatePoppler(poppler_w);
			dc_openPl.start();
			return TRUE;
		}
		else
		{
			setPrivateInt(getSkinName(), "pledit poppler width", dualwnd.getPosition());
			wdh_pl.hide();
			dualwnd.setPosition(0);
			return FALSE;
		}
	}
}

main.onAction (String action, String param, Int x, int y, int p1, int p2, GuiObject source)
{
	if (action == "load_comp" && param == "pledit")
	{
		int poppler_w = getPrivateInt(getSkinName(), "pledit poppler width", 200);
		if (dualwnd.getPosition() > 0) poppler_w = dualwnd.getPosition();
		updatePoppler(poppler_w);
		dc_openPl.start();
	}
}

dualwnd.onAction (String action, String param, Int x, int y, int p1, int p2, GuiObject source)
{
	if (action == "set_maxwidth")
	{
		updatePoppler(dualwnd.getPosition());
	}
}

wdh_pl.onSetVisible (Boolean onoff)
{
	debugString("wdh_pl set visible: " + integerToString(onoff), 9);
}

updatePoppler(int w)
{
	if (dualwnd.getPosition() == 0) return;
	int min_w = stringToInteger(dualwnd.getXmlParam("minwidth"));
	int max_w = stringToInteger(dualwnd.getXmlParam("maxwidth"));
	if (max_w < 0) max_w = dualwnd.getWidth() + max_w;
	debugString("max_w: " + integerToString(max_w), 9);
	boolean reset = FALSE;
	reset += (w == 0);
	reset += (w < min_w);
	if (reset) w = min_w;
	if (w > max_w) w = max_w;
	debugString("set poppler: " + integerToString(w), 9);
	dualwnd.setPosition(w);
}

dc_openPl.onTimer ()
{
	dc_openPl.stop();
	debugString("dc_openPl called!", 9);
	wdh_pl.show();
	g_playlist.show();
}

dc_closePl.onTimer ()
{
	dc_closePl.stop();
	wdh_pl.hide();
}

playlist_modulovar_attrib.onDataChanged ()
{
	resize_modulo = stringToInteger(GetData()) + 1;
}
